


import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

extension StringUtil on String {
  /// toast提示框
  toast({
    // Duration(seconds: 2) 秒
    Duration? duration,
    // EasyLoadingToastPosition.center
    // top,center,bottom,
    EasyLoadingToastPosition? toastPosition,
  }) {
    EasyLoading.showToast(this,
        duration: duration, toastPosition: toastPosition);
  }

  /// toast提示框横屏
  toastH({
    // Duration(seconds: 2) 秒
    Duration? duration,
    // EasyLoadingToastPosition.center
    // top,center,bottom,
    EasyLoadingToastPosition? toastPosition,
  }) {
    EasyLoading.showToast(this,
        duration: duration,
        toastPosition: toastPosition,
        landscapeScreen: true);
  }

  /// md5 加密
  String get md5Encrypt {
    var content = const Utf8Encoder().convert(this);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }

  /// 切割小数位数
  String toStringAsFixed(int fractionDigits) {
    String valueString = this;
    String doubleString =
        valueString.substring(valueString.indexOf('.') + 1, valueString.length);
    String intString = valueString.substring(0, valueString.indexOf('.'));
    if (doubleString.length < fractionDigits) {
      String zero = '';
      for (int i = 0; i < (fractionDigits - doubleString.length); i++) {
        zero += '0';
      }
      return '$valueString$zero';
    } else {
      doubleString = doubleString.substring(0, fractionDigits);
      return '$intString.$doubleString';
    }
  }
}
